package org.kms.Controllers;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.kms.pojo.KmsPermission;
import org.kms.pojo.KmsUser;
import org.kms.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/User")
@CrossOrigin(value = "*")
public class User {
    @Autowired
    private org.kms.dao.kmsUserDao kmsUserDao;

    @PostMapping("/userLogin")
    @ResponseBody
    public ResponseResult userlogin(String username, String password,HttpServletRequest request)throws Exception{
      /*  System.out.println(userpd);
        System.out.println(username);*/
 /*       String n = "";
        String upd = "";
        if (StringUtils.isEmpty(username)){
          n = request.getParameter("usernames");
          upd = request.getParameter("userpd");
            if (upd.equals("true")){
                username = n;
            }
      }*/
        KmsUser kmsUser =kmsUserDao.getbyUserName(username);
        if (kmsUser == null){
            return new ResponseResult(1,"用户名或者密码错误");
        }

            String reqPassword =new Md5Hash(password,kmsUser.getSalt()).toString();
            if (!kmsUser.getPassword().equals(reqPassword)){
                return new ResponseResult(1,"用户名或者密码错误");
            }

        List<KmsPermission> kmsPermissions =kmsUserDao.getbyUserId(kmsUser.getUserId());
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("kmsUser",kmsUser);
        map.put("kmsPermissions",kmsPermissions);
        return new ResponseResult(map);
    }
}
